@ECHO OFF

rem ===========================================================================
rem rem TESTED ON: openssl-1.0.0a
rem REQUIRES: cygwin: sed, bash
rem           perl for windows (tested on StrawberryPerl)
rem           nasm (Netwide Assembler)
rem           visual studio
rem PATH: bash, nasm, perl, sed
rem HOWTO: INSTALL.W32, INSTALL.W64
rem ===========================================================================

rem =====SETTINGS==============================================================

rem Library architecture, possible values: Win32, x64
set ARCH=Win32

rem Build debug or release version, possible values: Debug, Release
rem Debug build works only after release, perhaps there is some bug in configure
rem script in debug mode. Debug compilation after Release seems to create 
rem fine libs.
set BUILD_TYPE=Debug

rem Library type, possible values: Static, Dynamic
set LIBRARY_TYPE=Static

rem Visual C++ version, possible values: 8, 9, 10
set VC_VERSION=10

rem Visual Studio path
set VC_PATH=C:\Program Files (x86)\Microsoft Visual Studio 10.0

rem OpenSSL sources path
set OPENSSL_SRC_PATH=%CD%\src\openssl-1.0.0c

rem Temp directory without spaces in path
set TMP_DIR=c:\x\tmp

rem Directory where openssl will be installed
set INSTALL_ROOT=%CD%

rem Path to return after compilation
set RETURN_PATH=%CD%

rem ===========================================================================

set INSTALL_PATH=%INSTALL_ROOT%\vc_%VC_VERSION%\%ARCH%\%BUILD_TYPE%\%LIBRARY_TYPE%\openssl


rem http://msdn.microsoft.com/en-us/library/x4d2c09s(v=VS.80).aspx
if %ARCH% == Win32 (
    call "%VC_PATH%\VC\bin\vcvars32.bat"
) else (
    call "%VC_PATH%\VC\vcvarsall.bat" x86_amd64
)



cd %OPENSSL_SRC_PATH%

if %BUILD_TYPE% == Debug (
    set BUILD_TYPE_PREFIX=debug-VC
) else (
    set BUILD_TYPE_PREFIX=VC
)


if %ARCH% == Win32 (
    perl Configure %BUILD_TYPE_PREFIX%-WIN32 --prefix=%TMP_DIR%\openssl
    call ms\do_nasm.bat
) else (
    perl Configure %BUILD_TYPE_PREFIX%-WIN64A --prefix=%TMP_DIR%\openssl
    call ms\do_win64a.bat

    rem http://stackoverflow.com/questions/158232/how-do-you-compile-openssl-for-x64
    rem linker includes inc32 dir, it's the same, so won't touch it
    if %LIBRARY_TYPE% == Static (
        sed -b -i "s/OUT_D=out32/OUT_D=out64/" ms\nt.mak
        sed -b -i "s/TMP_D=tmp32/TMP_D=tmp64/" ms\nt.mak
    ) else (
        sed -b -i "s/OUT_D=out32dll/OUT_D=out64dll/" ms\ntdll.mak
        sed -b -i "s/TMP_D=tmp32dll/TMP_D=tmp64dll/" ms\ntdll.mak
    )
)


rem tests use bash's fc command
if %LIBRARY_TYPE% == Static (
    sed -b -i "s/ASM=nasmw/ASM=nasm/" ms\nt.mak
    nmake -f ms\nt.mak
    bash -c "nmake -f ms/nt.mak test"
    nmake -f ms\nt.mak install
) else (
    sed -b -i "s/ASM=nasmw/ASM=nasm/" ms\ntdll.mak
    nmake -f ms\ntdll.mak
    bash -c "nmake -f ms/ntdll.mak test"
    nmake -f ms\ntdll.mak install
)


xcopy "%TMP_DIR%\openssl" "%INSTALL_PATH%" /e /i /s /y
rmdir "%TMP_DIR%\openssl" /S /Q


cd %RETURN_PATH%

@ECHO ON
